package com.dgshanger.sanhxiaofeisc.ui.person.purse

import android.annotation.SuppressLint
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.HistoryPaidEntity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import kotlinx.android.synthetic.main.item_history_aid_list_.view.*


/**
 * HistoryPaidListFragment
 * 沉迷学习不能自拔
 * Describe：历史代付列表
 * Created by 雷小星🍀 on 2018/4/18 13:38.
 */
class HistoryPaidListFragment : BaseMvpRecycleViewFragment<HistoryPaidEntity>() {

    override val listLimit: Int
        get() = 10
    override val layoutManager: RecyclerView.LayoutManager
        get() = LinearLayoutManager(mContext)

    override fun createAdapter(): BaseRecycleAdapter<HistoryPaidEntity, out BaseViewHolder<HistoryPaidEntity>> = Adapter()

    override fun createModel(): BaseMvpRecycleViewContract.Model<HistoryPaidEntity> = Model()

    inner class Model : BaseMvpRecycleViewContract.Model<HistoryPaidEntity> {
        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<HistoryPaidEntity>>?) {
            AppApi.Api()
                    .otherpay_history()
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)

        }

    }

    override fun initEvent(view: View?) {
        super.initEvent(view)
        //设置上层回调
        adapter?.setOnItemClickListener { position, entity ->
            onItemClickListener?.onItemClick(entity)
        }
    }

    class Adapter : BaseRecycleAdapter<HistoryPaidEntity, ViewHolder>() {

        override fun createViewHolder(view: View, viewType: Int): ViewHolder = ViewHolder(view)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_history_aid_list_

    }

    class ViewHolder(view: View) : BaseViewHolder<HistoryPaidEntity>(view) {
        @SuppressLint("SetTextI18n")
        override fun bindView(position: Int, entity: HistoryPaidEntity?) {
            with(entity!!) {
                itemView.tv_user_name.text = rcvd_user_name
                itemView.tv_user_mobile.text = rcvd_user_mobile
                ImageLoader.loadAvatar(itemView.context, itemView.civ_avatar, rcvd_user_avatar)
            }
        }

    }

    interface OnItemClickListener {
        fun onItemClick(entity: HistoryPaidEntity)
    }

    var onItemClickListener: OnItemClickListener? = null
}